package Fly.example.controller;


import Fly.example.common.BaseContext;
import Fly.example.common.R;
import Fly.example.pojo.ShoppingCart;
import Fly.example.service.IShoppingCartService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author 陽Fly
 * @since 2025-06-19
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private IShoppingCartService shoppingCartService;

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        log.info("购物车业务开始…………");
        //获取当前用户的id
        Long userid = BaseContext.getCurrentid();
        //根据用户id查询购物车
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(userid!=null,ShoppingCart::getUserId, userid);
        wrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }

    //购物车添加功能
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("添加购物车的业务功能开始…………");
        //添加用户id
        shoppingCart.setUserId(BaseContext.getCurrentid());
        log.info("菜品信息为{}，来自前端发送的请求", shoppingCart);
        //查询当前购物车是否有当前添加的菜
        //判断当前添加的是菜品还是套餐
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentid());
        Long dishId = shoppingCart.getDishId();
        if(dishId != null){
            wrapper.eq(ShoppingCart::getDishId, dishId);
        }else {
            //如果是空，代表传过来的是套餐
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart cart = shoppingCartService.getOne(wrapper);
        if(cart!=null){
            //如果有就将number加1即可，相当于这份菜点了两次
            cart.setNumber(cart.getNumber()+1);
            shoppingCartService.updateById(cart);
        }else {
            //如果没有就添加
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cart = shoppingCart;
        }

        return R.success(cart);
    }

    //减少菜单的业务
    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        log.info("接收到前端发送过来的数据为：{}<UNK>", shoppingCart);
        //获取当前菜品id，如果菜品id为空，则表示当前传过来的是套餐的id
        Long dishId = shoppingCart.getDishId();
        if(dishId != null){
            //不等与空则表示当前要减少的是菜品
            LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ShoppingCart::getDishId, dishId);
            ShoppingCart dishcart = shoppingCartService.getOne(wrapper);
            if (dishcart.getNumber() != 1){
                //如果当前的数量不为1，就在当前的基础上减一
                dishcart.setNumber(dishcart.getNumber() - 1);
                //更新数据
                shoppingCartService.updateById(dishcart);
            }else{
                //如果等于1就直接删除
                shoppingCartService.removeById(dishcart);
            }
        }else {
            //如果等于空则表示当前传过来的是套餐
            LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
            ShoppingCart setmealcart = shoppingCartService.getOne(wrapper);
            if (setmealcart.getNumber() != 1){
                //如果当前套餐的数量不等于1则减一
                setmealcart.setNumber(setmealcart.getNumber() - 1);
                shoppingCartService.updateById(setmealcart);
            }else {
                //如果等于1则直接删除
                shoppingCartService.removeById(setmealcart);
            }
        }
        return R.success("减少成功");
    }

    //清空购物车
    @DeleteMapping("/clean")
    public R<String> clean(){
        log.info("清空购物车业务开始…………");
        //获取用户id
        Long userid = BaseContext.getCurrentid();
        //创建条件，根据用户id查询
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //根据用户id清空购物车
        wrapper.eq(ShoppingCart::getUserId, userid);
        shoppingCartService.remove(wrapper);
        return R.success("清空购物车成功");
    }
}
